package com.travel.server.manager.impl.command.user;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.user.QueryPermissionRequest;
import com.travel.server.manager.dto.response.user.QueryPermissionResponse;
import com.travel.server.manager.dto.response.user.QueryPermissionResponse.FirstMenuItem;
import com.travel.server.manager.dto.response.user.QueryPermissionResponse.MenuItem;
import com.travel.server.manager.dto.response.user.QueryPermissionResponse.SecondMenuItem;
import com.travel.server.manager.dto.response.user.QueryPermissionResponse.ThirdMenuItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentMenuDal;
import com.travel.server.manager.impl.dal.DepartmentMenuDal.DepartmentMenuParam;
import com.travel.server.manager.impl.dal.MenuDal;
import com.travel.server.manager.impl.sal.MenuSal;
import com.travel.server.manager.impl.vo.MenuVo;
import com.travel.server.manager.model.DepartmentMenu;
import com.travel.server.manager.model.MenuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/20 14:46
 * @Desc:
 */
@Component
public class QueryPermissionCommand extends AbstractCommand<QueryPermissionRequest, QueryPermissionResponse> {

    @Autowired
    private MenuDal menuDal;

    @Autowired
    private DepartmentMenuDal departmentMenuDal;

    @Override
    protected void checkInput(QueryPermissionRequest request) {

    }

    @Override
    public QueryPermissionResponse handle(QueryPermissionRequest request) {
        //查询所有的权限
        QueryPermissionResponse response = new QueryPermissionResponse();
        List<FirstMenuItem> list = new ArrayList<>();
        List<MenuInfo> menuList = menuDal.selectByExample(null);
        if(!CollectionUtil.isEmpty(menuList)){
            List<MenuVo> menuVoList = MenuSal.makeMenu(menuList);
            Map<Integer, Integer> map = selectDepartmentPermission(request.getId());
            list = makeMenu(menuVoList, map);
        }
        response.setFirstMenuList(list);
        return response;
    }

    private Map<Integer, Integer> selectDepartmentPermission(Integer departmentId){
        Map<Integer, Integer> map = new HashMap<>();
        if(departmentId != null) {
            DepartmentMenuParam param = new DepartmentMenuParam();
            param.setDepartmentId(departmentId);
            List<DepartmentMenu> list = departmentMenuDal.selectByExample(param);
            if (!CollectionUtil.isEmpty(list)) {
                for (DepartmentMenu departmentMenu : list) {
                    map.put(departmentMenu.getMenuId(), CommonConstant.YES);
                }
            }
        }
        return map;
    }

    private List<FirstMenuItem> makeMenu(List<MenuVo> list, Map<Integer, Integer> map){
        List<FirstMenuItem> firstMenuList = new ArrayList<>();
        for(MenuVo first : list){
            FirstMenuItem firstItem = toFirstMenuItem(makeMenuItem(first, map));
            List<SecondMenuItem> secondMenuList = new ArrayList<>();
            List<MenuVo> secondVoList = first.getChildList();
            if(!CollectionUtil.isEmpty(secondVoList)) {
                for (MenuVo second : secondVoList) {
                    SecondMenuItem secondItem = toSecondMenuItem(makeMenuItem(second, map));
                    List<ThirdMenuItem> thirdMenuList = new ArrayList<>();
                    List<MenuVo> thirdVoList = second.getChildList();
                    if (!CollectionUtil.isEmpty(thirdVoList)) {
                        for (MenuVo third : thirdVoList) {
                            ThirdMenuItem thirdMenuItem = toThirdMenuItem(makeMenuItem(third, map));
                            List<MenuItem> buttonMenuList = new ArrayList<>();
                            List<MenuVo> buttonVoList = third.getChildList();
                            if(!CollectionUtil.isEmpty(buttonVoList)){
                                for(MenuVo button : buttonVoList){
                                    MenuItem menuItem = makeMenuItem(button, map);
                                    buttonMenuList.add(menuItem);
                                }
                            }
                            thirdMenuItem.setButtonMenuList(buttonMenuList);
                            thirdMenuList.add(thirdMenuItem);
                        }
                    }
                    secondItem.setThirdMenuList(thirdMenuList);
                    secondMenuList.add(secondItem);
                }
            }
            firstItem.setSecondMenuList(secondMenuList);
            firstMenuList.add(firstItem);
        }
        return firstMenuList;
    }

    private FirstMenuItem toFirstMenuItem(MenuItem menuItem){
        FirstMenuItem firstMenuItem = new FirstMenuItem();
        firstMenuItem.setIsChoose(menuItem.getIsChoose());
        firstMenuItem.setMenuId(menuItem.getMenuId());
        firstMenuItem.setMenuName(menuItem.getMenuName());
        return firstMenuItem;
    }

    private SecondMenuItem toSecondMenuItem(MenuItem menuItem){
        SecondMenuItem secondMenuItem = new SecondMenuItem();
        secondMenuItem.setIsChoose(menuItem.getIsChoose());
        secondMenuItem.setMenuId(menuItem.getMenuId());
        secondMenuItem.setMenuName(menuItem.getMenuName());
        return secondMenuItem;
    }

    private ThirdMenuItem toThirdMenuItem(MenuItem menuItem){
        ThirdMenuItem thirdMenuItem = new ThirdMenuItem();
        thirdMenuItem.setIsChoose(menuItem.getIsChoose());
        thirdMenuItem.setMenuId(menuItem.getMenuId());
        thirdMenuItem.setMenuName(menuItem.getMenuName());
        return thirdMenuItem;
    }

    private MenuItem makeMenuItem(MenuVo vo, Map<Integer, Integer> map){
        MenuItem menuItem = new MenuItem();
        if(map.get(vo.getMenuId()) != null){
            menuItem.setIsChoose(CommonConstant.YES);
        }else {
            menuItem.setIsChoose(CommonConstant.NO);
        }
        menuItem.setMenuId(vo.getMenuId());
        menuItem.setMenuName(vo.getMenuName());
        return menuItem;
    }
}
